/**
 * Copyright (c) 2020, www.tellhowater.com (howaterdev@163.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hs.framework.opcClient.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import hs.framework.opcClient.entity.SourceParamB;
import hs.framework.opcClient.framework.util.Condition;
import hs.framework.opcClient.framework.util.Func;
import hs.framework.opcClient.framework.util.Query;
import hs.framework.opcClient.framework.util.R;
import hs.framework.opcClient.service.ISourceParamBService;
import hs.framework.opcClient.utils.ParamDataUtil;
import hs.framework.opcClient.wrapper.SourceParamBWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.html.parser.Entity;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据源信息表,用于对接控制数据 控制器
 *
 * @author Blade
 * @since 2021-05-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sourceparamb")
@Api(value = "数据源信息表,用于对接控制数据", tags = "数据源信息表,用于对接控制数据接口")
public class SourceParamBController {

	private ISourceParamBService sourceParamBService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入sourceParamB")
	public R<SourceParamB> detail(SourceParamB sourceParamB) {
		SourceParamB detail = sourceParamBService.getOne(Condition.getQueryWrapper(sourceParamB));
		return R.data(SourceParamBWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 数据源信息表,用于对接控制数据
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入sourceParamB")
	public R<IPage<SourceParamB>> list(SourceParamB sourceParamB, Query query) {
		IPage<SourceParamB> pages = sourceParamBService.page(Condition.getPage(query), Condition.getQueryWrapper(sourceParamB));
		return R.data(SourceParamBWrapper.build().pageVO(pages));
	}
	/**
	 * 自定义分页 数据源信息表,用于对接控制数据
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入sourceParamB")
	public R<IPage<SourceParamB>> page(SourceParamB sourceParamB, Query query) {
		IPage<SourceParamB> pages = sourceParamBService.selectSourceParamBPage(Condition.getPage(query), sourceParamB);
		return R.data(pages);
	}

	/**
	 * 新增 数据源信息表,用于对接控制数据
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入sourceParamB")
	public R save(@Valid @RequestBody SourceParamB sourceParamB) {
		return R.status(sourceParamBService.save(sourceParamB));
	}

	/**
	 * 修改 数据源信息表,用于对接控制数据
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入sourceParamB")
	public R update(@Valid @RequestBody SourceParamB sourceParamB) {
		return R.status(sourceParamBService.updateById(sourceParamB));
	}

	/**
	 * 新增或修改 数据源信息表,用于对接控制数据
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入sourceParamB")
	public R submit(@Valid @RequestBody SourceParamB sourceParamB) {
		return R.status(sourceParamBService.saveOrUpdate(sourceParamB));
	}

	
	/**
	 * 删除 数据源信息表,用于对接控制数据
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(sourceParamBService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 同步opc服务器数据
	 */
	@PostMapping("/updateOpcBuildingControl")
	@ApiOperationSupport(order = 9)
	public R updateOpcBuildingControl(){
		String st = ParamDataUtil.readFileContent("E:\\navicat\\Navicat Premium12-中文破解版\\sql\\dataitems-楼控.json");
		JSONArray array = JSONArray.parseArray(st);
		HashMap<String,SourceParamB> map = ParamDataUtil.getAllBuildingControl(array);
		//找主键id
		map = getSourceParamBByCode(map);
		//最后一起存储
		return R.status(sourceParamBService.updateBatchById(ParamDataUtil.mapToList(map)));
	}

	/**
	 * 同步opc服务器数据
	 */
	@PostMapping("/updateOpcLightControl")
	@ApiOperationSupport(order = 10)
	public R updateOpcLightControl(){
		String st = ParamDataUtil.readFileContent("E:\\navicat\\Navicat Premium12-中文破解版\\sql\\dataitems照明-2全部.json");
		JSONArray array = JSONArray.parseArray(st);
		HashMap<String,SourceParamB> map = ParamDataUtil.getAllLightControl(array);
		//找主键id
		map = getSourceParamBByCode(map);
		//最后一起存储
		return R.status(sourceParamBService.updateBatchById(ParamDataUtil.mapToList(map)));
	}

	/**
	 * 初始化opc服务器数据
	 */
	@PostMapping("/initOpcLightControl")
	@ApiOperationSupport(order = 11)
	public R initOpcLightControl(){
		String st = ParamDataUtil.readFileContent("E:\\navicat\\Navicat Premium12-中文破解版\\sql\\dataitems照明-2全部.json");
		JSONArray array = JSONArray.parseArray(st);
		HashMap<String,SourceParamB> map = ParamDataUtil.getAllLightControl(array);
		return R.status(sourceParamBService.saveBatch(ParamDataUtil.mapToList(map)));
	}

	public HashMap<String,SourceParamB> getSourceParamBByCode(HashMap<String,SourceParamB> map){
		//循环一遍   找到每个item在数据库里的主键id
		for (Map.Entry<String, SourceParamB> entity:map.entrySet()){
			SourceParamB sourceParamB = entity.getValue();
			//由于是更新值  不能直接getOne   要先由code和name来查找
			QueryWrapper<SourceParamB> sourceParamBQueryWrapper = new QueryWrapper<>();
			sourceParamBQueryWrapper.eq("code",sourceParamB.getCode());
			//将查找到的id赋值给处理过后的实体类
			sourceParamB.setId(sourceParamBService.getOne(sourceParamBQueryWrapper).getId());
			entity.setValue(sourceParamB);
		}
		return map;
	}
}
